package com.anmory;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ResponseController {
    @RequestMapping("/response")
    public void response(HttpServletResponse response) throws IOException {
        // 设置响应行
        response.setStatus(200);
        // 设置响应头
        response.setHeader("Content-Type", "text/html;charset=UTF-8");
        response.setHeader("Server", "Apache");
        response.setHeader("Connection", "keep-alive");
        response.setHeader("Date", "Wed, 08 Nov 2023 07:06:09 GMT");
        response.setHeader("Keep-Alive", "timeout=5, max=100");
        response.setHeader("X-Powered-By", "Jetty(9.4.46.v20220331)");
        response.setHeader("Content-Length", "12");
        response.setHeader("Vary", "Accept-Encoding");
        // 设置响应体
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("<h1>Response</h1>");
    }

    @RequestMapping("/response2")
    public ResponseEntity<String> response2(){
        return ResponseEntity.status(401)
                .header("Content-Type", "text/html;charset=UTF-8")
                .body("<h1>Response2</h1>");
    }

}
